﻿using Abp.Domain.Repositories;
using Abp.Domain.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace shenghua.Marketing.Coupons
{
    public class GrantCouponLogManager: DomainService
    {
        private readonly IRepository<GrantCouponLog> _grantCouponLogRepository;

        public GrantCouponLogManager(
            IRepository<GrantCouponLog> grantCouponLogRepository
            )
        {
            _grantCouponLogRepository = grantCouponLogRepository;
        }

        public async Task<GrantCouponLog> Add(Member.Members.Member member, Coupon coupon)
        {
            GrantCouponLog grantCouponLog = new GrantCouponLog
            {
                MemberId = member.Id,
                MemberUserName = member.UserName,
                CouponId = coupon.Id,
                CouponName = coupon.Name
            };
            await _grantCouponLogRepository.InsertAsync(grantCouponLog);
            return grantCouponLog;
        }

        public async Task<bool> IsGrantedForMember(long memberId, int couponId)
        {
            int count = _grantCouponLogRepository.GetAll().Where(r => r.MemberId == memberId && r.CouponId == couponId).Count();
            return count > 0;
        }
    }
}
